/*
 * @Author: DUST
 * @Date: 2023-05-29 17:05:52
 * @LastEditors: DUST
 * @LastEditTime: 2023-06-07 10:35:07
 * @FilePath: \koa-node-ts\app\service\TagsService.ts
 * @Description:
 * undefined
 */

import Tags from "../model/Tags";
import { tagItem } from "../validator/TagsValidator";

class TagsService {
  getTagList() {
    return Tags.findAll({
      attributes: ["name", "id", "sortNum", "createTime", "upTime"],
      order: [
        ["sortNum", "DESC"],
        ["createTime", "DESC"],
      ],
    });
  }
  getTagByIds(ids: Array<number>) {
    return Tags.findAll({
      attributes: ["name", "id"],
      where: {
        id: ids,
      },
    });
  }
  getTagByName(name: string) {
    return Tags.findOne({
      where: {
        name: name,
      },
    });
  }
  addTag(values: any) {
    return Tags.findOrCreate({
      where: { name: values.name },
      defaults: values
    });
  }
  editTag(values: tagItem, id: number) {
    return Tags.update(values, {
      where: {
        id,
      },
    });
  }

  deleteTag(id: number) {
    return Tags.destroy({ where: { id } });
  }
}

export default new TagsService();
